package com.example.heimashop.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.heimashop.R;
import com.example.heimashop.model.Floor;

import java.util.ArrayList;
import java.util.List;

public class FloorAdapter extends RecyclerView.Adapter<FloorAdapter.ViewHolder> {

    private List<Floor> floorList = new ArrayList<>();
    private FloorProductAdapter.OnItemClickListener listener;

    public void setOnItemClickListener(FloorProductAdapter.OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setData(List<Floor> list) {
        this.floorList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_floor, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Floor floor = floorList.get(position);
        if (floor.getFloorTitle() != null) {
            holder.tvFloorName.setText(floor.getFloorTitle().getName());
        }

        // 设置商品列表
        FloorProductAdapter productAdapter = new FloorProductAdapter();
        productAdapter.setData(floor.getProductList());
        productAdapter.setOnItemClickListener(listener);
        holder.rvProducts.setLayoutManager(new GridLayoutManager(holder.itemView.getContext(), 2));
        holder.rvProducts.setAdapter(productAdapter);
    }

    @Override
    public int getItemCount() {
        return floorList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvFloorName;
        RecyclerView rvProducts;

        ViewHolder(View view) {
            super(view);
            tvFloorName = view.findViewById(R.id.tv_floor_name);
            rvProducts = view.findViewById(R.id.rv_products);
        }
    }
}
